home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 168 / bug.c < prev    next >
C/C++ Source or Header  |  1988-03-31  |  2KB  |  97 lines

  1. /* help for debugging, use special ttp.s */
  2.  
  3. #define super(stk) trap(1, 0x20, stk)
  4.  
  5. struct dump {
  6.     long    magic;
  7.     long    data[8];
  8.     long    addr[8];
  9.     long     pc;
  10.     long    usp;
  11.     int    stk[4];
  12.     int    status;
  13.     long    upc;
  14.     int    more[8];
  15. } d;
  16.  
  17. struct header {
  18.     short    magic;
  19.     long    text;
  20.     long    data;
  21.     long    bss;
  22.     long    sym;
  23.     long    rsvd0;
  24.     long    rsvd1;
  25.     short    rsvd2;
  26. } h;
  27.  
  28. struct sym {
  29.     char     name[8];
  30.     short    type;
  31.     long    value;
  32. } s;
  33.  
  34. long    tpa, upc;
  35.  
  36. main(argc, argv) char *argv[]; {
  37.     extern long *_estk;
  38.     printf("tpa=%lx\n", tpa = *_estk);
  39.     getdump();
  40.     showdump();
  41.     if (argc > 1)
  42.         getfnc(argv[1], upc - (tpa + 0x100));
  43. }
  44.  
  45. getdump() {
  46.     long stk;
  47.     char *src, *dst;
  48.     int sz;
  49.     stk = super(0L);
  50.     mcopy(&d, 0x380L, sizeof d);
  51.     super(stk);
  52. }
  53.  
  54. showdump() {
  55.     int i;
  56.     long n;
  57.     if (d.magic != 0x12345678) {
  58.         printf("no dump available\n");
  59.         return;
  60.     }
  61.     printf("exception #%d\n", (short)(d.pc >> 24));
  62.     printf("   data     address\n");
  63.     for (i = 0; i < 8; i++)
  64.         printf("%d: %8lx %8lx\n", i, d.data[i], d.addr[i]);
  65.     printf("pc=%lx usp=%lx upc=%lx\n", 
  66.         d.pc & 0xFFFFFF, d.usp, upc = d.upc);
  67. }
  68.  
  69. getfnc(name, pc) char *name; long pc; {
  70.     int fd;
  71.     long n, lseek();
  72.     struct sym fnc;
  73.     if ((fd = open(name, 0)) < 0)
  74.         printf("can't open %s\n", name);
  75.     else if (read(fd, &h, sizeof h) != sizeof h)
  76.         printf("can't read header of %s\n", name);
  77.     else if (h.sym == 0L)
  78.         printf("no symbols in %s\n", name);
  79.     else if (lseek(fd, h.text+h.data, 1) < 0L)
  80.         printf("premature EOF on %s\n", name);
  81.     else    {
  82.         *fnc.name = 0;
  83.         fnc.value = 0;
  84.         for (n = h.sym; n > 0; n -= sizeof s) {
  85.             read(fd, &s, sizeof s);
  86.             if (s.value > fnc.value && s.value <= pc)
  87.                 mcopy(&fnc, &s, sizeof fnc);
  88.         }
  89.         printf("bug at %ld words after '%s'\n",
  90.             pc - fnc.value, fnc.name);
  91.     }
  92. }
  93.  
  94. mcopy(dst, src, sz) char *dst, *src; {
  95.     while (sz--) *dst++ = *src++;
  96. }
  97.